package com.hanggold.quartz;

import com.hanggold.quartz.job.HelloJob;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

/**
 * @author <a href="mailto:sunmch@163.com">smc</a>
 * @date 2019-08-22 14:49
 * @since 1.0.0
 **/
public class QuartzOneDemo {

    public static void main(String[] args) {
        try {
            new QuartzOneDemo().startJob();
        } catch (Exception se) {
            se.printStackTrace();
        }
    }


    public void startJob() throws SchedulerException, InterruptedException {

        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();

        JobDetail  jobDetail =  newJob(HelloJob.class)
//                .withIdentity("job1", "group1")
                .storeDurably()
                .build();

        Trigger trigger = newTrigger()
//                .withIdentity("job1", "group1")
//                .forJob(jobDetail.getKey())
                .startNow()
//                .forJob(jobDetail)
                .withSchedule(simpleSchedule()
                        .withIntervalInSeconds(2)
                        .repeatForever())
                .build();


//        JobDetail  jobDetail2 =  newJob(HelloJob.class)
////                .withIdentity("job1", "group1")
//                .storeDurably()
//                .build();
//
//        Trigger trigger2 = newTrigger()
//                .withIdentity("job1", "group1")
////                .forJob(jobDetail.getKey())
//                .startNow()
////                .forJob(jobDetail)
//                .withSchedule(simpleSchedule()
//                        .withIntervalInSeconds(2)
//                        .repeatForever())
//                .build();



//        scheduler.addJob(jobDetail,true);

        scheduler.scheduleJob(jobDetail,trigger);

        scheduler.scheduleJob(jobDetail2,trigger2);
//        scheduler.

        scheduler.start();

//        System.out.println("10秒钟暂停");
//        Thread.sleep(10000L);
//
//        //
//        scheduler.standby();
//
////        scheduler.interrupt(jobDetail.getKey())
//
//        System.out.println("开始停止");
//        Thread.sleep(10000L);
//
//        scheduler.start();
//        System.out.println("已经启动项目");

    }


}
